package com.chuanyang.driverh.mainorder;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.bean.AssignGoodData;
import com.chuanyang.driverh.mainorder.adapter.AssignGoodDtailNewAdapter;
import com.chuanyang.driverh.responseresult.OrderDetailData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.PrintUtil;

import java.util.ArrayList;

public class AssignGoodsDetailNewActivity extends BaseTitleActivity {

    private ArrayList<AssignGoodData.WaybillDetailsData> waybillDetails;
    private AssignGoodDtailNewAdapter assignGoodsAdapter;
    private OrderDetailData orderDetailData;
    private AssignGoodData assignGoodData;
    private ListView lv_assigingoods_list;
    private TextView tv_assigngooddetail_realweight;
    private TextView tv_assigngooddetail_drivername;
    private TextView tv_assigngooddetail_carcode;
    private ImageView iv_assigngooddetail_pepople;
    private LinearLayout ll_assigngooddetail_div;
    private Intent intent;
    // private EditText et_assigngooddetail_count;
    private String assignweight;
    private String type;

    @Override
    protected int getContentResId() {
        return R.layout.order_act_assigngoodsdetail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        lv_assigingoods_list = (ListView) findViewById(R.id.lv_assigngooddetail_list);
        ll_assigngooddetail_div = (LinearLayout) findViewById(R.id.ll_assigngooddetail_div);
        tv_assigngooddetail_realweight = (TextView) findViewById(R.id.tv_assigngooddetail_realweight);//实际载重
        tv_assigngooddetail_drivername = (TextView) findViewById(R.id.tv_assigngooddetail_drivername);
        tv_assigngooddetail_carcode = (TextView) findViewById(R.id.tv_assigngooddetail_carcode);
        iv_assigngooddetail_pepople = (ImageView) findViewById(R.id.iv_assigngooddetail_pepople);
    }

    @Override
    protected void initData() {
        intent = getIntent();
        assignGoodData = (AssignGoodData) intent.getSerializableExtra("AssignGoodData");
        orderDetailData = (OrderDetailData) intent.getSerializableExtra("OrderDetailData");
        //"1"修改    2添加
        type = intent.getStringExtra("type");
        setBackImg();
        getTitleTextView().setText("分配");
        getRightTextView().setText("确认");

        if (type.equals("1")) {
            for (OrderDetailData.DetailListBean detailListBean : orderDetailData.getDetailList()) {

                for (int a = 0; a < assignGoodData.getWaybillDetails().size(); a++) {
                    AssignGoodData.WaybillDetailsData waybillDatails = assignGoodData.getWaybillDetails().get(a);
                    waybillDetails = assignGoodData.getWaybillDetails();
                    if (detailListBean.getOrderDetailId().equals(waybillDetails.get(a).getOrderdetailId()))//同一种货物
                    {
                        if (!TextUtils.isEmpty(waybillDatails.getDetailsWeight())) {
                            waybillDatails.setTotalWeight(Double.parseDouble(detailListBean.getSurplusWeight()) + Double.parseDouble(waybillDatails.getDetailsWeight()));
                        }
                        waybillDetails.set(a, waybillDatails);

                    }
                }

            }
            assignGoodData.setWaybillDetails(waybillDetails);
        }

        fillView();
        createList();
    }

    @Override
    protected void initListener() {

    }

    private void fillView() {
        waybillDetails = new ArrayList();
        fillHeader();
        assignGoodsAdapter = new AssignGoodDtailNewAdapter(this, waybillDetails);
        lv_assigingoods_list.setAdapter(assignGoodsAdapter);
    }


    private void fillHeader() {
        BitmapUtil.showImgForNetPath(assignGoodData.getDriverImage(), iv_assigngooddetail_pepople);
        tv_assigngooddetail_realweight.setText(assignGoodData.getLoadWeight() + "");
        tv_assigngooddetail_carcode.setText(assignGoodData.getPlateNumber());
        tv_assigngooddetail_drivername.setText(assignGoodData.getDriverName());
    }

    private void createList() {

        if (assignGoodData.getWaybillDetails() != null)//点击修改时
        {
            waybillDetails = assignGoodData.getWaybillDetails();
        } else {
            for (OrderDetailData.DetailListBean detailListBean : orderDetailData.getDetailList()) {
                AssignGoodData.WaybillDetailsData bean = new AssignGoodData.WaybillDetailsData();
                //bean.setTotalWeight(Double.parseDouble(detailListBean.getGoodsWeight()));//剩余货物重量(21之前代码)
                bean.setTotalWeight(Double.parseDouble(detailListBean.getSurplusWeight()));
                bean.setOrderdetailId(detailListBean.getOrderDetailId());
                bean.setGoodsStandard(detailListBean.getGoodsStandard());
                bean.setGoodName(detailListBean.getGoodsName());
                waybillDetails.add(bean);
            }
        }
        assignGoodsAdapter.setList(waybillDetails);
    }

    @Override
    protected void baseGoEnsure() {
        double totalweight = 0;
        waybillDetails = (ArrayList<AssignGoodData.WaybillDetailsData>) assignGoodsAdapter.getList();
        //由于此处没有分配时，会出现空的数据 所以需要剔除
        if (waybillDetails.size() > 0) {
            for (int i = 0; i < waybillDetails.size(); i++) {
                AssignGoodData.WaybillDetailsData waybillDetail = waybillDetails.get(i);
                if (TextUtils.isEmpty(waybillDetail.getDetailsWeight())) {
//                        waybillDetails.remove(waybillDetail);
//                        i--; // 因为位置发生改变，所以必须修改i的位置
                } else {
                    try {
                        assignweight = waybillDetail.getDetailsWeight();
                        if (assignweight.endsWith(".")) {//如果尾数为""
                            assignweight = assignweight.substring(assignweight.length() - 1);
                            waybillDetail.setDetailsWeight(assignweight);
                            waybillDetails.set(i, waybillDetail);
                        }
                        if( waybillDetail.getTotalWeight()<Double.parseDouble(assignweight)) {
                            PrintUtil.toast(activityContext,waybillDetail.getGoodName()+"分配货物超过该货物总量");
                            return;
                        }
                        totalweight += Double.parseDouble(assignweight);
                    } catch (Exception e) {
                        PrintUtil.toast(activityContext, "请检查您的输入");
                        return;
                    }
                }
            }
            if (totalweight == 0) {
                PrintUtil.toast(activityContext, "您分配的货物重量为零，无法提交");
                return;

            }


        }
        if (totalweight > assignGoodData.getLoadWeight()) {//当前运单重量超过司机运力
            PrintUtil.toast(activityContext, "分配的货物重量不能大于司机运力");
            return;
        }
        assignGoodData.setWaybillDetails(waybillDetails);
        assignGoodData.setAssignWeight(totalweight);//司机已被分配的货物总重量
        // assignGoodData.setAmount(et_assigngooddetail_count.getText().toString());
        Intent intent = new Intent();
        intent.putExtra("AssignGoodData", assignGoodData);
        if (type.equals("2")) {//添加
            setResult(2, intent);
        } else {
            setResult(3, intent);//修改

        }
        finish();

    }
}
